/****************************************************
 * 创建人: @author zoujian    
 * 创建时间: 2021-6-23/15:52
 * 项目名称: design_pattern
 * 文件名称: CursorTestController.java
 * 文件描述: @Description: 流程查询
 *
 * All rights Reserved, Designed By 投资交易团队
 * @Copyright:2016-2021
 *
 ********************************************************/
package com.zj.mybatis.cursor;

import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 包名称：com.zj.mybatis.cursor
 * 类名称：CursorTestController
 * 类描述：流程查询
 * 创建人：@author zoujian
 * 创建时间：2021-6-23/15:52
 */
@RestController
@RequestMapping("/cursor")
public class CursorTestController {

    @Autowired
    private CursorMapper cursorMapper;
    @Autowired
    private PlatformTransactionManager transactionManager;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    @GetMapping("foo/scan/0/{limit}")
    @Transactional(rollbackFor = Exception.class)
    public void scanFoo0(@PathVariable("limit") int limit) throws Exception {
        TransactionTemplate transactionTemplate =
                new TransactionTemplate(transactionManager);

        transactionTemplate.execute(status -> {
            try (
                    Cursor<String> cursor = cursorMapper.scan(limit)
                    ) {
                cursor.forEach(foo -> {
                    System.out.println(foo);
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        });
    }

    @GetMapping("foo/scan/1/{limit}")
    public void scanFoo1(@PathVariable("limit") int limit) throws Exception {
        try (
                SqlSession sqlSession = sqlSessionFactory.openSession();
                Cursor<String> cursor =
                        sqlSession.getMapper(CursorMapper.class).scan(limit)
        ) {
            cursor.forEach(foo -> { });
        }
    }
}
